package com.bwie.controller;

import com.alibaba.fastjson.JSONObject;
import com.bwie.pojo.Order;
import com.bwie.pojo.User;
import com.bwie.service.OrderService;
import com.bwie.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private OrderService orderService;

    //前往帮好友砍价页面
    @RequestMapping("/goHelp")
    public String goHelp(){
        return "help";
    }

    //生成好友的砍价链接
    @RequestMapping("/link")
    @ResponseBody
    public String link(Integer orderId){
        String s = redisTemplate.opsForValue().get("user");
        User user = JSONObject.parseObject(s, User.class);
        Integer userId = user.getUserId();
        return "http://localhost:8082/record/goHelp?userId="+userId+"&orderId="+orderId;
    }

    //判断这个订单是否存在
    @RequestMapping("/findOrder")
    @ResponseBody
    public int findOrder(Integer userId,Integer orderId){
        Order order = orderService.findOrder(orderId,userId);
        if (order==null){
            //不存在则告知没有此订单
            return 0;
        }
        return 1;
    }

    //订单不存在提示页面
    @RequestMapping("/goNull")
    public String goNull(){
        return "null";
    }
}
